# coding: utf-8
from PySide6.QtCore import Qt, QPropertyAnimation, QPoint
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QFrame
from qfluentwidgets import ClickableSlider, FluentStyleSheet, HollowHandleStyle
from qfluentwidgets.multimedia import MediaPlayerBase
from qfluentwidgets.multimedia.media_play_bar import PlayButton, VolumeButton


class MediaPlayBarBase(QFrame):
    """ Play bar base class """

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.player = None  # type: MediaPlayerBase

        self.playButton = PlayButton(self)
        self.volumeButton = VolumeButton(self)
        self.progressSlider = ClickableSlider(Qt.Orientation.Horizontal, self)

        self.posAni = QPropertyAnimation(self, b'pos', self)
        self.posAni.setDuration(250)

        self.__initWidgets()

    def __initWidgets(self):
        self.progressSlider.setStyle(HollowHandleStyle(
            {
                "groove.height": 4,
                "sub-page.color": QColor(72, 210, 242),
                "add-page.color": QColor(255, 255, 255, 50),
                "handle.color": QColor(72, 210, 242),
                "handle.ring-width": 2,
                "handle.hollow-radius": 10,
                "handle.margin": 0
            }
        ))

        FluentStyleSheet.MEDIA_PLAYER.apply(self)

        self.playButton.clicked.connect(self.togglePlayState)

    def setMediaPlayer(self, player: MediaPlayerBase):
        """ set media player """
        self.player = player

        self.player.durationChanged.connect(self.progressSlider.setMaximum)
        self.player.positionChanged.connect(self._onPositionChanged)
        self.player.mediaStatusChanged.connect(self._onMediaStatusChanged)
        self.player.volumeChanged.connect(self.volumeButton.setVolume)
        self.player.mutedChanged.connect(self.volumeButton.setMuted)

        # don't use valueChanged, otherwise the sound will be interupted
        self.progressSlider.sliderMoved.connect(self.player.setPosition)
        self.progressSlider.clicked.connect(self.player.setPosition)
        self.volumeButton.volumeChanged.connect(self.player.setVolume)
        self.volumeButton.mutedChanged.connect(self.player.setMuted)

        self.player.setVolume(30)

    def fadeIn(self):
        self.posAni.setStartValue(self.pos())
        self.posAni.setEndValue(self.pos() + QPoint(0, -100))
        self.posAni.start()

    def fadeOut(self):
        self.posAni.setStartValue(self.pos())
        self.posAni.setEndValue(self.pos() + QPoint(0, 100))
        self.posAni.start()

    def play(self):
        self.player.play()

    def pause(self):
        self.player.pause()

    def stop(self):
        self.player.stop()

    def setVolume(self, volume: int):
        """ Sets the volume of player """
        self.player.setVolume(volume)

    def setPosition(self, position: int):
        """ Sets the position of media in ms """
        self.player.setPosition(position)

    def _onPositionChanged(self, position: int):
        self.progressSlider.setValue(position)

    def _onMediaStatusChanged(self, status):
        self.playButton.setPlay(self.player.isPlaying())

    def togglePlayState(self):
        """ toggle the play state of media player """
        if self.player.isPlaying():
            self.player.pause()
        else:
            self.player.play()

        self.playButton.setPlay(self.player.isPlaying())
